package com.atwork.wuhua.base;

import android.app.Application;
import android.content.Context;

import com.atwork.wuhua.bean.UserBean;
import com.atwork.wuhua.jpush.MyJPushManager;
import com.atwork.wuhua.mvp.DataModel;
import com.atwork.wuhua.mvp.Token;
import com.atwork.wuhua.mvp.presenter.HomePagePresenter;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.SPUtils;
import com.atwork.wuhua.utils.UserUtil;
import com.atwork.wuhua.mvp.Callback;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;

import cn.jpush.android.api.JPushInterface;

public class MyApplication extends Application {
    private static MyApplication instance;
    public static UserBean userBean;
    public static SystemBean systemBean;
    public static String UUID = "";
    public static String AUTH = "";
    public static String IMEI = "";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
    }

    // 获取Application
    public static Context getMyApplication() {
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    private void init() {
        //在使用SDK各组件之前初始化context信息，传入ApplicationContext
        SDKInitializer.initialize(this);
        //自4.3.0起，百度地图SDK所有接口均支持百度坐标和国测局坐标，用此方法设置您使用的坐标类型.
        //包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);

        IMEI = JPushInterface.getUdid(this);

        if (UserUtil.isLogin(this)) {
            userBean = UserUtil.getUserBean(this);
            UUID = userBean.getData().getUuid();
            AUTH = userBean.getData().getAuth();
            getInfo();
        }


        //极光初始化
        JPushInterface.setDebugMode(true);
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);
        MyJPushManager.getInstance().initPush(this);

        getLatLog();
        LogUtils.e("包名==>" + getPackageName());
    }


    /**
     * 获取用户信息
     */
    public void getInfo() {
        if (UserUtil.isLogin(this)) {
            BaseReq req = new BaseReq();
            req.setKey("uuid", MyApplication.UUID);
            if (req != null) {
                DataModel.request(Token.API_MYINFO)
                        .params(req, false)
                        .execute(new Callback<UserBean>() {


                            @Override
                            public void onSuccess(String msg, UserBean data) {
                                UserUtil.loginSuccess(getApplicationContext(), data);

                            }

                            @Override
                            public void onFailure(String msg) {

                            }

                            @Override
                            public void onError() {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        }
    }

    /**
     * 获取当前位置的经纬度
     */
    private LocationClient mLocClient;
    private MyLocationListenner myListener = new MyLocationListenner();

    public void getLatLog() {
        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式 高精度
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setIsNeedAddress(false);// 设置定位结果包含地址信息
        mLocClient.setLocOption(option);
        mLocClient.start();
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null) {
                return;
            }
            LogUtils.e("latmain==>" + location.getLatitude());
            LogUtils.e("logmain==>" + location.getLongitude());
            SPUtils.setString(getApplicationContext(), SPUtils.LONGITUDE, location.getLongitude() + "");
            SPUtils.setString(getApplicationContext(), SPUtils.LATITUDE, location.getLatitude() + "");

        }
    }
}
